package com.example.myapplication.activity.Niubi_activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.myapplication.R;

public class SpinnerDropdownActivity extends AppCompatActivity {

    private final String[] array = {"水星", "金星", "地球", "火星", "木星", "土星", "天王星", "海王星", "冥王星"};
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_dropdown);
        spinner = findViewById(R.id.spinner_dropdown);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_select, array);
        spinner.setAdapter(adapter);
        // 设置默认选中项
        spinner.setSelection(0);
        // 为下拉列表设置各种事件的响应，这个事响应菜单被选中
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(android.widget.AdapterView<?> parent, View view, int position, long id) {
                // 提示信息
                Toast.makeText(SpinnerDropdownActivity.this, "您选择的是：" + array[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(android.widget.AdapterView<?> parent) {
                Log.i("Spinner", "onNothingSelected: ");
            }
        });
    }
}